package fm.dongman.animefm.view.fragment

import android.app.Fragment
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.bumptech.glide.Glide
import java.util.ArrayList
import fm.dongman.animefm.R
import fm.dongman.animefm.contractlib.IModel
import fm.dongman.animefm.contractlib.contract.UserInfoContract
import fm.dongman.animefm.view.activity.SingleActivity
import fm.dongman.animefm.view.adapter.UserDetailsAdapter
import fm.dongman.animefm.view.util.SystemHelper
import fm.dongman.animefm.view.util.ToastHelper
import fm.dongman.animefm.view.widget.MultiStateLayout
import fm.dongman.modellib.StaticArea.Injection
import fm.dongman.modellib.model.UserModel
import fm.dongman.presenterlib.presenter.UserInfoPresenter
import kotlinx.android.synthetic.main.fragment_user_details.*
import kotlinx.android.synthetic.main.layout_user_details.*
import kotlinx.android.synthetic.main.layout_user_info_head.*
import org.jetbrains.anko.onClick

/**
 * 用户详情页Fragment
 * Created by Administrator on 2017/10/20.
 */
class UserInfoFragment : BaseFragment(), UserInfoContract.IUserInfoView {
    private lateinit var mPresenter: UserInfoContract.IUserInfoPresenter
    private var mListFrag: MutableList<Fragment> = ArrayList()
    private var mTitle: MutableList<String> = ArrayList()
    private lateinit var mUser: UserModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null) {
            mUserId = arguments.getString(mUserId)
        }
    }

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val view = inflater!!.inflate(R.layout.fragment_user_details, container, false)
        UserInfoPresenter(this, Injection.provideUserInfoRepository())
        return view
    }

    override fun initData() {
        onStateChanged(MultiStateLayout.LOADING)
        mTitle.clear()
        mListFrag.clear()
        mTitle.add("收藏的专题")
        mTitle.add("关注的漫单")
        mTitle.add("发表的评论")
        mListFrag.add(SheetListFragment.getInstance(SheetListFragment.TYPE_USER_COL, mUserId))
        mListFrag.add(SheetListFragment.getInstance(SheetListFragment.TYPE_USER_FOLLOW, mUserId))
        mListFrag.add(CommentListFragment.getInstance(CommentListFragment.TYPE_OTHERS, mUserId))
        mTbLogin.setPadding(0, SystemHelper.getStatusBarHeight(activity), 0, 0)
    }

    override fun bindData() {
        mVpUserInfo.adapter = UserDetailsAdapter(fragmentManager, mTitle, mListFrag)
        mVpUserInfo.offscreenPageLimit = 3
        mTluserInfoTabLayout!!.setupWithViewPager(mVpUserInfo)
        // 返回按钮
        mTvLoginCancel.onClick {
            activity.finish()
        }
        //关注该用户
        mBtnUserInfofollow.onClick {

            if (mBtnUserInfofollow.text == "关注") {
                mPresenter.followUser(mUserId, UserInfoContract.IUserInfoPresenter.IS_FOLLOWED)
            } else {
                mPresenter.followUser(mUserId, UserInfoContract.IUserInfoPresenter.NOT_FOLLOWED)
            }
        }
        //跳转私信界面
        mBtnUserInfoPrivateletter.onClick {
            val intent = Intent(activity, SingleActivity::class.java)
            intent.putExtra(SingleActivity.EXTRA_NAME, SingleActivity.INTENT_EXTRA_CHAT)
            intent.putExtra(SingleActivity.BUNDLE_EXTRA_KEY_TYPE, mUserId)
            intent.putExtra(SingleActivity.BUNDLE_EXTRA_KEY_NAME, mUser.userName)
            activity.startActivity(intent)
        }
    }

    override fun onStart() {
        super.onStart()
        mPresenter.getUserInfo(mUserId)
    }

    override fun setPresenter(presenter: UserInfoContract.IUserInfoPresenter) {
        mPresenter = presenter
    }

    override fun showNoNetwork() {
        onStateChanged(MultiStateLayout.NET_ERROR)
    }

    override fun showRefreshFailed() {
        onStateChanged(MultiStateLayout.EMPTY)
        ToastHelper.getInstance(activity).showError(getString(R.string.str_refresh_failed))
    }

    override fun showUserInfo(userInfo: IModel.IUserModel) {
        onStateChanged(MultiStateLayout.SUCCESS)
        mUser = (userInfo as UserModel)
        // 设置是否关注
        if (mUser.getmIsFollow() == "1") {
            mBtnUserInfofollow.text = "取关"
        } else {
            mBtnUserInfofollow.text = "关注"
        }
        Glide.with(activity).load(mUser.avatarUrl).asBitmap().into(mTvUserInfoHeadImg)//头像
        mTvUserInfoName.text = mUser.userName           //用户名
        mTvUserInfoFollow.text = mUser.flowNum         //关注
        mTvUserInfoFans.text = mUser.fansNum           //粉丝
        Log.i("-0------aa", mUser.avatarUrl)
        if ("" == mUser.description) {
            mTvAutograph.text = "该用户很懒，暂无签名！"
        } else {
            mTvAutograph.text = mUser.description //签名
        }

    }

    override fun showFollowSucceed() {
        if (mBtnUserInfofollow.text == "关注") {
            mBtnUserInfofollow.text = "取关"
        } else {
            mBtnUserInfofollow.text = "关注"
        }
        ToastHelper.getInstance(activity).showSucceed("操作成功")
    }

    override fun showFollowFailed() {
        ToastHelper.getInstance(activity).showError("操作失败")
    }

    override fun isActive(): Boolean = isAdded

    override fun getViewContext(): Context = activity

    /**
     * 切换状态
     */
    private fun onStateChanged(state: Int) {
        mUserInfoMultiState?.changeState(state)
    }

    companion object {
        var mUserId: String = ""
        private var INSTANCE: UserInfoFragment? = null
        fun getInstance(flag: String): UserInfoFragment {
            if (INSTANCE == null) {
                INSTANCE = UserInfoFragment()
            }
            val args = Bundle()
            args.putString(mUserId, flag)
            INSTANCE!!.arguments = args
            return INSTANCE!!
        }
    }
}
